const fs = require("fs-extra")
const execa = require("execa")

const run = (bin, args, opts = {}) => execa(bin, args, { stdio: "inherit", ...opts })

async function buildProject() {
  try {
    await run("npm", ["run", "build"])
  } catch (error) {
    throw error
  }
}

async function moveArticlesIntoDistDir() {
  //copy articles dir's file  into  dist/articles
  fs.copy("articles", "dist/articles", function (err) {
    if (err) {
      console.log("An error occured while copying the folder.")
      return console.error(err)
    }
    console.log("Copy completed!")
  })
}

async function moveEssaysIntoDistDir() {
  //copy essays dir's file  into  dist/essays
  fs.copy("essays", "dist/essays", function (err) {
    if (err) {
      console.log("An error occured while copying the folder.")
      return console.error(err)
    }
    console.log("Copy completed!")
  })
}

async function main() {
  await buildProject()
  await moveArticlesIntoDistDir()
  await moveEssaysIntoDistDir()
}

main()
